package br.com.paiva.container.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.paiva.container.Perfil;
import br.com.paiva.container.dao.PerfilDAO;
import br.com.paiva.container.service.PerfilService;

@Service("perfilService")
public class PerfilServiceImpl implements PerfilService{

	@Autowired
	private PerfilDAO perfilDAO;	
	
	public void setPerfilDAO(PerfilDAO perfilDAO) {
		this.perfilDAO = perfilDAO;
	}
	
	@Transactional(readOnly=true)
	public Perfil getPerfilById(Long id) {		
		return this.perfilDAO.retrive(id);
	}
	
	@Transactional(readOnly=true)
	public List<Perfil> listPerfil() {		
		return this.perfilDAO.listAll();
	}
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void savePerfil(Perfil perfil) {
		this.perfilDAO.save(perfil);		
	}
	
	@Transactional(readOnly=true)
    public List<SelectItem> getPerfilItemCombo(){
		
        List<Perfil> itens = this.perfilDAO.listAll();
		
        List<SelectItem> listaItem = new ArrayList<SelectItem>();
		
		for(Perfil p : itens){		   
		   listaItem.add(new SelectItem(p.getId().toString(), p.getNivel()));
		}
		
		return listaItem;
	}
}
